package com.candy.control;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/login")
public class LoginController{

    @RequestMapping("/login.do")
    public ModelAndView show1(){
        System.out.println("接受到前端发送过来的请求");
        ModelAndView mv=new ModelAndView();
//        会将视图名字根据视图解析器配置的前缀后缀进行自动拼接
//        /WEB-INF/pages/welcome.jsp
        mv.setViewName("welcome");
        mv.addObject("msg","SpringMVC的第一个程序");
        return mv;
    }
//ResponseBody注解：告诉spring将该list以json字符串的方式返回
    @RequestMapping("/show2.do")
    @ResponseBody
    public List<Map<String,Object>> show2(){
        List<Map<String,Object>> lmap=new ArrayList<>();
        Map<String,Object> map=new HashMap<>();
        map.put("name","tom");
        map.put("age",21);
        lmap.add(map);
        return lmap;
    }

    /**
     * 页面跳转，直接在创建视图模型是指定视图名字即可
     * @return
     */
    @RequestMapping("/show3.do")
    public ModelAndView show3(){
        ModelAndView mv=new ModelAndView("welcome");
        return mv;
    }

    /**
     * 当返回值为String类型是，则返回的内容为视图名字
     * @return
     */
    @RequestMapping("/show4.do")
    public String show4(){
        return "welcome";
    }


    /**
     * 当返回值为String类型是，则返回的内容为视图名字
     * 可以增加model作为参数，进行数据传输
     * @return
     */
    @RequestMapping("/show5.do")
    public String show5(Model model){
        model.addAttribute("msg","SpringMVC程序...");
        return "welcome";
    }

    /**
     * 获取表单传递的参数
     * @return
     */
    @RequestMapping("/show6.do")
    public String show6(@RequestParam("username") String username,@RequestParam("userpwd") String userpwd,Model model){
        System.out.println(username);
        System.out.println(userpwd);
        model.addAttribute("msg",username);
        return "welcome";
    }


}
